﻿using XiaoMan.Model.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseDAO<T> where T : class
    {
        private XiaoManS10Context context = DbContextFactory.GetDbContext();
        /// <summary>
        /// 通用新增
        /// </summary>
        /// <param name="entity"></param>
        public void Add(T entity)
        {
            context.Set<T>().Add(entity);
        }
        /// <summary>
        /// 通用批量新增
        /// </summary>
        /// <param name="entity"></param>
        public void AddRange(List<T> entitys)
        {
            context.Set<T>().AddRange(entitys);
        }
        /// <summary>
        /// 通用删除
        /// </summary>
        /// <param name="entity"></param>
        public void Delete(T entity)
        {
            context.Set<T>().Remove(entity);
        }
        /// <summary>
        /// 通用批量删除
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteRange(List<T> entitys)
        {
            context.Set<T>().RemoveRange(entitys);
        }
        /// <summary>
        /// 通用修改
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="modifiedEntity"></param>
        public void Update(T entity)
        {
            context.Entry(entity).State = EntityState.Modified;
        }
        /// <summary>
        /// 通用查询
        /// </summary>
        /// <param name="lambadaWhere"></param>
        /// <returns></returns>
        public List<T> GetList(Func<T, bool> lambadaWhere)
        {
            return context.Set<T>().Where(lambadaWhere).ToList();
        }
        /// <summary>
        /// 通用分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="lambadaWhere" value="分页范围,对所有还是对部分"></param>
        /// <param name="lambadaOrdey" value="分页的哪个个字段进行排序"></param>
        /// <param name="isAsc" value="是否升序"></param>
        /// <param name="pageIndex" value="页码"></param>
        /// <param name="pageSize" value="每页的条数"></param>
        /// <param name="Count" value="总条数"></param>
        /// <returns></returns>
        public List<T> GetListOrdeyByPage<S>(Func<T, bool> lambadaWhere, Func<T, S> lambadaOrdey, bool isAsc, int pageIndex, int pageSize, ref int Count)
        {
            List<T> temp = context.Set<T>().Where(lambadaWhere).ToList();
            //   pageCount = temp.Count % pageSize == 0 ? temp.Count / pageSize : temp.Count / pageSize + 1;
            Count = temp.Count;
            if (isAsc)
            {
                return temp.OrderBy(lambadaOrdey)
                    .Skip((pageIndex - 1) * pageSize)
                    .Take(pageSize).ToList();
            }
            else
            {
                return temp.OrderByDescending(lambadaOrdey)
                    .Skip((pageIndex - 1) * pageSize)
                    .Take(pageSize).ToList();
            }
        }
    }
}
